package com.example.shop.shops.controller;

import com.example.shop.util.AjaxResult;
import com.example.shop.util.FastdfsUtil;
import org.apache.commons.io.FilenameUtils;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * fastdfs 文件上传接口
 */
@RestController
@RequestMapping("/fastdfs")
@CrossOrigin(origins = "*",maxAge = 3600)
public class FastdfsController {


    /**
     * 上传
     */
    @PostMapping("/upload")
    public AjaxResult upload(@RequestPart("file") MultipartFile file) throws IOException {
        /// 真实路径
        String originalFilename = file.getOriginalFilename();
        // 获取扩展名
        String extension = FilenameUtils.getExtension(originalFilename);
        // 执行上传
        String path = FastdfsUtil.upload(file.getBytes(), extension);
        return AjaxResult.me().success(path);
    }

    /**
     * 删除
     */

    @DeleteMapping
    public AjaxResult delete(@RequestParam("path") String path) {
        String allPath = path.substring(1);
        String groupName = allPath.substring(0, allPath.indexOf("/"));
        String pathName = allPath.substring(allPath.indexOf("/") + 1);
        FastdfsUtil.delete(groupName, pathName);
        return AjaxResult.me().success(path);
    }
}